package com.cheng.xbjy.dao;

import com.cheng.xbjy.entity.User;
import com.cheng.xbjy.entity.UserFocus;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.Date;
import java.util.List;

public interface UserDao extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {

    User findByEmail(String email);

    User findByUsername(String username);

    @Query("update User u set u.password = ?2 where u.email = ?1")
    @Modifying
    void updatePassword(String email, String password);

    @Query("update User u set u.loginTime = ?2 where u.id = ?1")
    @Modifying
    void updateLoginTime(Long id, Date date);

    Page<User> findByUsernameLike(String username, Pageable pageable);

    @Query("select uf.userFocusId from UserFocus uf where uf.userId = ?1")
    List<Long> findFocusByUserId(Long userId);

    @Query("select count(uf) from UserFocus uf where uf.userFocusId = ?1")
    Integer countFocusByUserId(Long id);

    @Query("update User u set u.look=u.look+1 where u.id = ?1")
    @Modifying
    void updateLookById(Long id);

    @Query("select uf from UserFocus  uf where uf.userId = ?1 and uf.userFocusId = ?2")
    UserFocus findByUserIdAndFocusId(Long userId, Long focusId);

    @Query("delete from UserFocus uf where uf.userId = ?1 and uf.userFocusId = ?2")
    @Modifying
    void deleteFocus(Long userId, Long focusId);

    @Query(value = "insert into userfocus values(?1,?2)", nativeQuery = true)
    @Modifying
    void addFocus(Long userId, Long focusId);

    @Query("update User u set u.pic = ?2 where u.id = ?1")
    @Modifying
    void updateUserPic(Long userId, String url);

    @Query("select u from User u join UserFocus uf on u.id = uf.userFocusId where uf.userId = ?1")
    Page<User> findFocusPage(Long userId, PageRequest of);

    @Query("select count(u) from User u where u.deptId = ?1")
    Integer countDeptUser(Long deptId);

    @Query("select u from User u where u.deptId = ?1")
    List<User> findDeptUser(Long deptId);


    User findByWxOpenid(String wxOpenid);

}
